<?php

class database_query
{
	var $mess, $rows = Array(), $replaceKeys = Array(), $column_titles, $column_values, $rank, $column_widths, $empty_mess;

	function database_query($title_message, $query, $column_titles, $column_values, $rank=false, $column_widths = '', $empty_mess = '')
	{
		global $db;
		$this->rank = $rank;
		$this->mess = $title_message;
		$this->column_titles = $column_titles;
		$this->column_values = $column_values;
		$this->column_widths = $column_widths;
		$this->empty_mess = $empty_mess;
		$replaceDone = false;
		$result = $db->sql_query($query);
		
		while($row = $db->sql_fetchrow($result))
		{
			if($replaceDone == false)
			{
				$replaceDone = true;
				foreach($row as $key => $value)
				{
					$this->replaceKeys[$key] = '{' . $key . '}';
				}
			}
			
			$this->rows[] = $row;
		} 
	}

	function render()
	{	
		global $skin_dir;
	
		$this->mess->render();
		
		if(sizeof($this->rows) > 0)
		{
			
			include $skin_dir . 'tpl/tableStartBuffered.tpl';
			
			include $skin_dir . 'tpl/rowStart.tpl';
			$class = 'tableHeadingLeft';
			$width = '';
			
			if($this->rank)
			{
				$value = '';
				include $skin_dir . 'tpl/tableCell.tpl';
			}
			
			for($x = 0; $x < sizeof($this->column_titles); $x++){
				$value = $this->column_titles[$x];
				if($this->column_widths != '')
					$width = ' WIDTH="' . $this->column_widths[$x] . '" ';
				else
					$width = '';
				include $skin_dir . 'tpl/tableCell.tpl';
			}
			include $skin_dir . 'tpl/rowEnd.tpl';
					
			$width = '';
			$on = false;
			$x = 1;
			foreach($this->rows as $row)
			{
				$row = formatArrayOutput($row);
				$class = $on ? 'tableEntryTextOn' : 'tableEntryTextOff';
				include $skin_dir . 'tpl/rowStart.tpl';
				
				if($this->rank)
				{
					$value = $x;
					include $skin_dir . 'tpl/tableCell.tpl';
				}
				
				foreach( $this->column_values as $value){
					$value = str_replace($this->replaceKeys, $row, $value);
					include $skin_dir . 'tpl/tableCell.tpl';
				}
				include $skin_dir . 'tpl/rowEnd.tpl';
				
				$on = ! $on;
				$x++;
			}
			include $skin_dir . 'tpl/tableEnd.tpl';
		}
		else
		{
			if($this->empty_mess != '')
				$this->empty_mess->render();
		}
	}
}
?>